﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
namespace FOBVT.Web.FOBVT2011.en
{
    public partial class Receive : System.Web.UI.Page
    {
        protected string v_oid;		// 订单号
        protected string v_pstatus;	// 支付状态码
        //20（支付成功，对使用实时银行卡进行扣款的订单）；
        //30（支付失败，对使用实时银行卡进行扣款的订单）；

        protected string v_pstring;	//支付状态描述
        protected string v_pmode;	//支付银行
        protected string v_amount;	//支付金额
        protected string v_moneytype;	//币种		
        protected string remark1;	// 备注1
        protected string remark2;	// 备注1
        protected string v_md5str;
        protected string status_msg;
        protected string str;	// 备注1
        protected void Page_Load(object sender, EventArgs e)
        {
            // MD5密钥要跟订单提交页相同，如Send.asp里的 key = "test" ,修改""号内 test 为您的密钥
            string key = "37DD5A0741613728";	// 如果您还没有设置MD5密钥请登陆我们为您提供商户后台，地址：https://merchant3.chinabank.com.cn/
            // 登陆后在上面的导航栏里可能找到“B2C”，在二级导航栏里有“MD5密钥设置”
            // 建议您设置一个16位以上的密钥或更高，密钥最多64位，但设置16位已经足够了

            v_oid = Request["v_oid"];
            v_pstatus = Request["v_pstatus"];
            v_pstring = Request["v_pstring"];
            v_pmode = Request["v_pmode"];
            v_md5str = Request["v_md5str"];
            v_amount = Request["v_amount"];
            v_moneytype = Request["v_moneytype"];
            //remark1 = Request["remark1"];
            //remark2 = Request["remark2"];

            string str = v_oid + v_pstatus + v_amount + v_moneytype + key;

            str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(str, "md5").ToUpper();

            if (str == v_md5str)
            {

                string customerID = v_oid.Substring(0, 12);
                if (v_pstatus.Equals("20"))
                {
                    //支付成功
                    //在这里商户可以写上自己的业务逻辑

                    UpdateInfo("4", "SubmitPaySuccessfully!", customerID, v_oid);
                   
                }
                else
                {
                    UpdateInfo("3", "SubmitFailed", customerID, v_oid);
                    status_msg = "Submit Info failed!";
                }
                System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
                FOBVT.Web.MainClass.GetUserInfo user = new FOBVT.Web.MainClass.GetUserInfo();
                user.Conn = Conn;
                FOBVT.Web.MainClass.User userInfo = user.GetUserInfoForPDF(customerID);
                FOBVT.Web.MainClass.SessionState.CurrentUser = userInfo;
                Response.Redirect("payment_success.aspx?customerID=" + customerID + "&oid=" + v_oid);
            }
            else
            {

                status_msg = "Submit Info failed!";
            }
        }


        private bool UpdateInfo(string statusID, string StatusValue, string customerID, string OrderId)
        {
            System.Data.OleDb.OleDbConnection Conn = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("..\\..\\DB\\FOBVTweb.mdb"));
            FOBVT.Web.MainClass.DBClass DBClassTmp = new FOBVT.Web.MainClass.DBClass();
            ArrayList sql = new ArrayList();
            sql.Clear();
            sql.Add(string.Format("UPDATE  CustomerInfo SET StatusID='{0}',Status='{1}',Barcode='{2}',PayTime=#" + System.DateTime.Now.ToString() +
              "# WHERE (CustomerID ='{3}')", statusID, StatusValue, OrderId, customerID));

            return DBClassTmp.ExcuteSQL(sql, Conn);

        }

    }
}
